package com.usian.wemedia.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.apis.wemedia.WmUserControllerApi;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/user")
public class WmUserController implements WmUserControllerApi {

    @Autowired
    private WmUserService wmUserService;

    @Override
    @PostMapping("/save")
    public ResponseResult save(@RequestBody WmUser wmUser) {
        boolean b = wmUserService.save(wmUser);
        if (b){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            return ResponseResult.okResult(AppHttpCodeEnum.PARAM_INVALID);
        }
    }

    @Override
    @GetMapping("/findByName/{name}")
    public WmUser findByName(@PathVariable String name) {
        List<WmUser> wmUsers = wmUserService.list(new QueryWrapper<WmUser>().eq("name", name));
        if (wmUsers.size()>0){
            return wmUsers.get(0);
        }
        return null;
    }


    /**
     * 根据自媒体用户id 查询该作者信息
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/findOne/{id}")
    public WmUser findWmUserById(@PathVariable Long id) {
        return wmUserService.getById(id);
    }
}
